import {createRouter, createWebHistory} from 'vue-router'
import Layout from "@/layout/Layout.vue";
import {useStore} from "vuex";
const store = useStore()
// meta: 如果想让该项在菜单中不显示 添加 visible: false  例如：添加首页点位
// 不可以只在这里加，因为登陆时后端有传侧边栏权限，所以需要去数据库表中添加

export const route = [
    //   监控首页只有header与body
    {
        path: '',
        name: '首页',
        component: Layout,
        redirect: '/home/home',
        children: [ // 动态菜单数据
            {
                path: '/home',
                name: '首页',
                children: [
                    {
                        path: 'home',
                        name: '首页',
                        meta: {
                            tracertTo: "/homePage",
                        }
                    },
                    {
                        path: 'homeSetting',
                        name: '首页设置',
                        component: () => import('../views/home/HomeSetting.vue'),
                    },
                ]
            },
            {
                path: "/client",
                name: '单位管理',
                children: [{
                    path: 'client',
                    name: '单位信息',
                    component: () => import('../views/client/Client.vue')
                },
                    {
                        path: 'equipmentFacilities',
                        name: '单位设施',
                        //对应 消防设施
                        component: () => import('../views/client/EquipmentFacilities.vue')
                    },
                ]
            },
            {
                path: "/user",
                name: '用户管理',
                children: [
                    {
                    path: 'user',
                    name: '用户',
                    component: () => import('../views/user/User.vue')
                }, {
                        path: 'role',
                        name: '角色',
                        component: () => import('../views/user/Role.vue')
                    },
                ]
            },
            {
                path: "/equipment",
                name: '设备管理',
                children: [
                    {
                        path: "facility",
                        name: '设备设施',
                        children: [
                            {
                                path: 'deviceList',
                                name: '电器火灾监测系统',
                                component: () => import('../views/equipment/facility/DeviceList.vue')
                            },
                            {
                                path: 'fireCockList',
                                name: '智能消火栓系统',
                                component: () => import('../views/equipment/facility/FireCockList.vue')
                            },
                            {
                                path: 'fireAlarmSystem',
                                name: '火灾自动报警系统',
                                children: [
                                    {
                                        path: 'fireAlarmConUnit',
                                        name: '集中火灾报警控制器',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/FireAlarmConUnit.vue')
                                    },
                                    {
                                        path: 'otherUnit',
                                        name: '其他设备',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/OtherUnit.vue')
                                    },
                                    {
                                        path: 'fireExtingSystem',
                                        name: '自动灭火系统',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/FireExtingSystem.vue')
                                    },
                                    {
                                        path: 'fireHydrantSystem',
                                        name: '消火栓系统',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/FireHydrantSystem.vue')
                                    },
                                    {
                                        path: 'safeEvacuatSystem',
                                        name: '安全疏散系统',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/SafeEvacuatSystem.vue')
                                    },
                                    {
                                        path: 'smokeConExhSystem',
                                        name: '防烟排烟系统',
                                        component: () => import('../views/equipment/facility/fireAlarmSystem/SmokeConExhSystem.vue')
                                    },
                                ]
                            },
                        ]
                    },
                    // {
                    //     path: 'equipmentUnit',
                    //     name: '设备部件',
                    //     //对应 消防部件
                    //     component: () => import('../views/equipment/EquipmentUnit.vue')
                    // },
                    {
                        path: 'deviceType',
                        name: '设备类型',
                        component: () => import('../views/equipment/DeviceType.vue')
                    },
                    // {
                    //     path: 'simList',
                    //     name: 'SIM卡列表',
                    //     //再加这一个页面
                    //     component: () => import('../views/equipment/SIMList.vue')
                    // },
                    // {
                    //     path: 'unitType',
                    //     name: '部件类型',
                    //     //再加这一个页面
                    //     component: () => import('../views/equipment/UnitType.vue')
                    // },
                    {
                        path: 'agreement',
                        name: '协议管理',
                        //对应 协议管理
                        component: () => import('../views/equipment/Agreement.vue')
                    },
                    // {
                    //     path: 'faultStatistics',
                    //     name: '故障统计',
                    //     component: () => import('../views/equipment/FaultStatistics.vue')
                    // },
                    // {
                    //     path: 'equipmentFailure',
                    //     name: '设备故障',
                    //     component: () => import('../views/equipment/EquipmentFailure.vue')
                    // },
                    {
                        path: '/fireSysData',
                        name: '报警控制设备日志',
                        component: () => import('../views/equipment/FireSysData.vue')
                    },
                    {
                        path: '/deviceSysData',
                        name: '电气火灾设备详情',
                        component: () => import('../views/equipment/sysData/DeviceSysData.vue')
                    },
                    {
                        path: '/deviceSysLog',
                        name: '电气火灾设备日志',
                        component: () => import('../views/equipment/sysData/DeviceSysLog.vue')
                    },
                    {
                        path: '/fireCockSysData',
                        name: '消火栓设备详情',
                        component: () => import('../views/equipment/sysData/FireCockSysData.vue')
                    },
                    {
                        path: '/fireCockSysLog',
                        name: '消火栓设备日志',
                        component: () => import('../views/equipment/sysData/FireCockSysLog.vue')
                    },
                    {
                        path: '/currentDqData',
                        name: '安全用电探测器详情',
                        component: () => import('../views/equipment/sysData/CurrentDqData.vue')
                    },
                    {
                        path: '/currentDqLog',
                        name: '安全用电探测器日志',
                        component: () => import('../views/equipment/sysData/CurrentDqLog.vue')
                    },
                    {
                        path: '/currentJhData',
                        name: '声光报警器详情',
                        component: () => import('../views/equipment/sysData/CurrentJhData.vue')
                    },
                    {
                        path: '/currentJhLog',
                        name: '声光报警器日志',
                        component: () => import('../views/equipment/sysData/CurrentJhLog.vue')
                    },
                    {
                        path: '/currentLsData',
                        name: '水压探测器详情',
                        component: () => import('../views/equipment/sysData/CurrentLsData.vue')
                    },
                    {
                        path: '/currentLsLog',
                        name: '水压探测器日志',
                        component: () => import('../views/equipment/sysData/CurrentLsLog.vue')
                    },
                    {
                        path: '/currentQgData',
                        name: '气感探测器详情',
                        component: () => import('../views/equipment/sysData/CurrentQgData.vue')
                    },
                    {
                        path: '/currentQgLog',
                        name: '气感探测器日志',
                        component: () => import('../views/equipment/sysData/CurrentQgLog.vue')
                    },
                    {
                        path: '/currentYgData',
                        name: '烟雾探测器详情',
                        component: () => import('../views/equipment/sysData/CurrentYgData.vue')
                    },
                    {
                        path: '/currentYgLog',
                        name: '烟雾探测器日志',
                        component: () => import('../views/equipment/sysData/CurrentYgLog.vue')
                    },
                ]
            },
            {
                path: "/warn",
                name: '报警管理',
                children: [
                    {
                        path: 'warnRecord',
                        name: '全部报警',
                        component: () => import('../views/warn/WarnRecord.vue')
                    },
                    {
                        path: 'warnType',
                        name: '报警类型',
                        component: () => import('../views/warn/WarnType.vue')
                    },
                    {
                        path: 'warnState',
                        name: '报警状态',
                        component: () => import('../views/warn/WarnState.vue')
                    },
                ]
            },
            {
                path: "/video",
                name: '视频监控',
                children: [
                    {
                        path: 'videoList',
                        name: '监控列表',
                        component: () => import('../views/video/VideoList.vue')
                    },
                ]
            },
            {
                path: "/statistics",
                name: '统计报表',
                children: [
                    // {
                    //     path: 'dailyElectricity',
                    //     name: '状态历史报表',
                    //     component: () => import('../views/statisticalForm/DailyElectricityStatistics.vue')
                    // },
                    {
                        path: 'hourElectricity',
                        name: '报警记录报表',
                        component: () => import('../views/statisticalForm/HourElectricityStatistics.vue')
                    },
                    // {
                    //     path: 'dailyFailure',
                    //     name: '电功率记录报表',
                    //     component: () => import('../views/statisticalForm/DailyFailureStatistics.vue')
                    // },
                    // {
                    //     path: 'dailyFailure',
                    //     name: '事件分析',
                    //     component: () => import('../views/statisticalForm/DailyFailureStatistics.vue')
                    // },
                ]
            },
            // {
            //     path: "/SmokeSensing",
            //     name: "烟感设备",
            //     children: [
            //         {
            //             path: 'listOfSmokeSensingDevices',
            //             name: '烟感设备列表',
            //             component: () => import('../views/smokeSensingEquipment/ListOfSmokeSensingDevices.vue')
            //         },
            //         {
            //             path: 'smokeSensingUser',
            //             name: '烟感用户',
            //             component: () => import('../views/smokeSensingEquipment/SmokeSensingUser.vue')
            //         },
            //     ]
            // },
            {
                path: "/record",
                name: '历史查询',
                children: [
                    {
                        path: 'inspectAddrCode',
                        name: '地址列表',
                        component: () => import('../views/record/InspectAddrCode.vue')
                    },
                    {
                        path: 'inspection',
                        name: '巡检记录',
                        component: () => import('../views/record/InspectionRecord.vue')
                    },
                    {
                        path: 'maintenance',
                        name: '维保记录',
                        component: () => import('../views/record/MaintenanceRecord.vue')
                    },
                    {
                        path: 'checkRecord',
                        name: '查岗记录',
                        component: () => import('../views/record/CheckRecord.vue')
                    },
                    {
                        path: 'notice',
                        name: '通知查询',
                        component: () => import('../views/record/NoticeRecord.vue')
                    },
                    {
                        path: 'operate',
                        name: '操作日志',
                        component: () => import('../views/record/OperateRecord.vue')
                    },
                    // {
                    //     path: 'record',
                    //     name: '受理记录',
                    //     component: () => import('../views/record/RecordAccept.vue')
                    // },
                    // {
                    //     path: 'recordEquipmentOperate',
                    //     name: '设施操作',
                    //     component: () => import('../views/record/RecordEquipmentOperate.vue')
                    // },
                ]
            },
            {
                path: "/contingencyPlan",
                name: '应急预案',
                children: [
                    {
                        path: 'ContingPlanTeam',
                        name: '应急预案小组',
                        component: () => import('../views/contingencyPlan/ContingPlanTeam.vue')
                    },
                    {
                        path: 'ContingPlan',
                        name: '消防应急预案',
                        component: () => import('../views/contingencyPlan/ContingPlan.vue')
                    },
                    {
                        path: 'ContingPlanFlow',
                        name: '应急预案流程',
                        component: () => import('../views/contingencyPlan/ContingPlanFlow.vue')
                    }
                ]
            },
            {
                path: "/fireControlTrain",
                name: '消防培训',
                children: [
                    {
                        path: 'productTechTrain',
                        name: '产品和技术培训',
                        component: () => import('../views/fireControlTrain/ProductTechTrain.vue')
                    },
                    {
                        path: 'fireLaws',
                        name: '消防法规',
                        //对应 消防法规
                        component: () => import('../views/fireControlTrain/FireLaws.vue')
                    },
                    {
                        path: 'fireComSense',
                        name: '消防常识',
                        //对应 消防常识
                        component: () => import('../views/fireControlTrain/FireComSense.vue')
                    },
                ]
            },
            {
                path: "/fireArchives",
                name: '消防档案',
                children: [
                    {
                        path: 'fireBasicInfor',
                        name: '消防安全基本情况',
                        component: () => import('../views/fireArchives/FireBasicInfor.vue')
                    },
                    {
                        path: 'fireManageInfor',
                        name: '消防安全管理情况',
                        component: () => import('../views/fireArchives/fireManageInfor.vue')
                    },
                ]
            },
            {
                path: "/securityReport",
                name: '安全报告',
                children: [
                    {
                        path: 'clientOperationReport',
                        name: '单位运行报告',
                        component: () => import('../views/securityReport/ClientOperationReport.vue')
                    },
                ]
            },
            // {
            //     path: '/shortNote',
            //     name: '短信通知',
            //     component: () => import('../views/ShortNote.vue'),
            // },
            {
                path: '/changePassword',
                name: '修改密码',
                component: () => import('../views/ChangePassword.vue'),
            },
            {
                path: "/addPoint",
                name: "添加首页点位",
                component: () => import("../views/homeComponents/AddPoint.vue"),
                meta: {
                    visible: false
                }
            },
        ]
    },

    {
        path: '/defaultHome',
        name: '默认首页',
        component: () => import('../views/DefaultHome.vue')
    },
    {
      path: "/pdf",
      name: "pdf",
      component: ()=> import('../template/ClientExportPdf.vue')
    },
    {
        path: '/homePage',
        name: '首页大屏',
        component: () => import('../views/home/Home.vue'),
    },
    {
        path: "/login",
        name: "login",
        component: () => import('../views/Login.vue')
    },
    {
        path: '/addrCode',
        name: '二维码',
        component: () => import('../views/qrcode/AddrCode.vue'),
    },
    {
        path: '/addrCodeAbout',
        name: '地址详情',
        component: () => import('../views/qrcode/AddrCodeAbout.vue'),
    }
]
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: route
})
router.beforeEach((to, from, next) => {
    // 1. 首先处理不需要认证的路由
    if (to.path === "/addrCode" || to.path === "/login") {
        next()
        return // 确保后续代码不会执行
    }

    // 2. 处理根路径重定向
    if (to.path === "/") {
        next("/homePage")
        return
    }

    // 3. 检查用户是否登录
    if (!localStorage.getItem('user')) {
        if (to.path !== "/login") {
            next('/login')
        } else {
            next()
        }
        return
    }

    // 4. 用户已登录，检查是否需要修改密码
    try {
        const user = JSON.parse(localStorage.getItem('user'))
        if (user.password === "e10adc3949ba59abbe56e057f20f883e" &&
            to.path !== '/changePassword') {
            next('/changePassword')
            return
        }
    } catch (e) {
        console.error('Error parsing user data:', e)
        next('/login')
        return
    }

    // 5. 所有检查通过，正常导航
    next()
})

export default router
